package com.appkubes.mall.admin.application.risk.service.impl;

import com.appkubes.mall.admin.application.risk.dao.RiskDao;
import com.appkubes.mall.admin.application.risk.entity.MallBaiRongVO;
import com.appkubes.mall.admin.application.risk.service.RiskService;
import com.br.client.facade.MerchantServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

@Service
public class RiskServiceImpl implements RiskService {
    @Autowired
    private RiskDao riskDao;
    /**
     * 定时更新token
     * @return
     */
    @Override
    public String BaiRongTiming() {
        RestTemplate restTemplate = new RestTemplate();
        //第一种 postForEntity 可以获取响应状态，头部信息等
        String url = "https://apiservice.100credit.com/api/auth-service/auth/getToken?username=fenfStr&password=fenfStr123";
        ResponseEntity<String> entity = restTemplate.postForEntity(url,"",String.class);
        String str = entity.getBody();
        System.err.println(str+"执行静态定时任务时间: " + new Date());
        return str.substring(25,57);
    }
    /**
     * 填写信息获取swit_number
     * @return
     */
    @Override
    public String StrategyDemoAppkey2(MallBaiRongVO mallBaiRongVO) {
        MerchantServer ms = new MerchantServer();
        net.sf.json.JSONObject jso = new net.sf.json.JSONObject();
        jso.put("apiName", "strategyApi");
        net.sf.json.JSONObject reqData = new net.sf.json.JSONObject();
        reqData.put("strategy_id", "STR0037570");
        reqData.put("id", mallBaiRongVO.getId());
        reqData.put("cell", mallBaiRongVO.getCell());
        reqData.put("name", mallBaiRongVO.getName());
        jso.put("reqData", reqData);
        //如果不传apicode和appkey则读取yml配置文件中的信息
        String apiData = ms.getApiData(jso.toString(), "3007099", "5a3bd822a9b4c6bd4c0e236786be3d025922c1976e905e97d3116eab48fd7b54");
        return apiData.substring(29,63);
    }
    @Override
    public Integer InsertBaiRong(MallBaiRongVO mallBaiRongVO) {
        return riskDao.InsertBaiRong(mallBaiRongVO);
    }
}
